package com.xxkj.drugtraceability.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.databinding.DialogBlueBinding
import com.xxkj.drugtraceability.databinding.DialogYellowBinding
import com.xxkj.drugtraceability.ext.clickN

/**
 * author: zhoujingjin
 * created on: 2024/9/11 13:48
 * description:
 */
class YellowDialog(context: Context, content:String, type:Int = 0, title:String="操作提示", oneButtonName:String="确定", action:()->Unit):AlertDialog(context) {
    init {
       val view =  LayoutInflater.from(context).inflate(R.layout.dialog_yellow,null)
       val bind = DataBindingUtil.bind<DialogYellowBinding>(view)
        bind?.apply {
            tvContent.text = content
            tvTitle.text = title
           when(type){
                0-> {
                    viewCenter.visibility = View.VISIBLE
                    tvConfirm.visibility = View.VISIBLE
                    tvConfirm.clickN {
                        action.invoke()
                        dismiss()
                    }
                }
               3->{
                   viewCenter.visibility = View.GONE
                   tvCancel.visibility = View.GONE
                   tvConfirm.clickN {
                       action.invoke()
                       dismiss()
                   }
               }
                else->{
                    viewCenter.visibility = View.GONE
                    tvConfirm.visibility = View.GONE
                    tvCancel.text = oneButtonName
                    tvCancel.clickN(action)
                }
            }
            tvCancel.setOnClickListener {
                dismiss()
            }
        }
        setView(view)
        window.setBackgroundDrawableResource(R.color.transparent)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        create()
        show()
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.width = (context.resources.displayMetrics.widthPixels * 0.8).toInt()  // 设置宽度为屏幕宽度的80%
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT  // 高度自适应
        window?.attributes = params  // 应用新的参数
        window?.setBackgroundDrawableResource(R.color.transparent)
    }
}